﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageBusinessSubCategories : System.Web.UI.Page
{
    #region Types

    public enum LayoutType
    {
        Add,
        Edit,
        Search,
        View
    }

    #endregion

    #region Constants

    private const string PROP_DATASRC_BUSINESSSUBCATEGORIES = "__BUSINESSSUBCATEGORIES::BUSINESSSUBCATEGORIES";
    private const string PROP_DATASRC_BUSINESCATEGORY = "__BUSINESCATEGORY::BUSINESCATEGORY";
    private const string PROP_LAYOUT = "__PAGEVIDEO::UIL";
    private const string PROP_SELECTED_BUSINESSSUBCATEGORY_ID = "__BUSINESSSUBCATEGORY::SBUSINESSSUBCATEGORYID";

    #endregion

    #region Public Properties

    public BusinessSubCategoryCollection DataSourceBusinessSubCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_BUSINESSSUBCATEGORIES] is BusinessSubCategoryCollection ? (BusinessSubCategoryCollection)SessionMgr.Session[PROP_DATASRC_BUSINESSSUBCATEGORIES] : (BusinessSubCategoryCollection)(SessionMgr.Session[PROP_DATASRC_BUSINESSSUBCATEGORIES] = new BusinessSubCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_BUSINESSSUBCATEGORIES] = value; }
    }

    public int SelectedBusinessSubCategoryID
    {
        get { return (SessionMgr.Session[PROP_SELECTED_BUSINESSSUBCATEGORY_ID] is int ? (int)SessionMgr.Session[PROP_SELECTED_BUSINESSSUBCATEGORY_ID] : 0); }
        set { SessionMgr.Session[PROP_SELECTED_BUSINESSSUBCATEGORY_ID] = value; }
    }


    public BusinessCategoryCollection DataSourceBusinessCategory
    {
        get { return (SessionMgr.Session[PROP_DATASRC_BUSINESCATEGORY] is BusinessCategoryCollection ? (BusinessCategoryCollection)SessionMgr.Session[PROP_DATASRC_BUSINESCATEGORY] : (BusinessCategoryCollection)(SessionMgr.Session[PROP_DATASRC_BUSINESCATEGORY] = new BusinessCategoryCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_BUSINESCATEGORY] = value; }
    }

    public LayoutType UILayout
    {
        get { return (SessionMgr.Session[PROP_LAYOUT] is LayoutType ? (LayoutType)SessionMgr.Session[PROP_LAYOUT] : LayoutType.Search); }
        set { SessionMgr.Session[PROP_LAYOUT] = value; }
    }




    #endregion

    #region Event Handlers

    #region Page Load

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Cache.SetCacheability(HttpCacheability.NoCache);

        if (!Page.IsPostBack)
        {
            DataSourceBusinessSubCategory = BusinessSubCategory.GetAllBusinessSubCategories();
            BusinessSubCategoryList1.DataBind(DataSourceBusinessSubCategory);
        }
    }

    #endregion

    #region Control: BusinessSubCategoryList

    protected void ucBusinessSubCategoryList_EditRequested(object sender, Controls_BusinessSubCategoryList.EditRequestedEventArgs evt)
    {
       SelectedBusinessSubCategoryID = evt.BSC.BusinessSubCatgryID;
       BusinessSubCategoryViewEdit1.DataBind(evt.BSC, DataSourceBusinessCategory);
        SetLayout(LayoutType.Edit);
        divexistbusinessSubcategoryname.Visible = false;
    }

    protected void ucBusinessSubCategoryList_AddNewRequested(object sender, EventArgs evt)
    {
        BusinessSubCategoryViewEdit1.CancelEdit();
        BusinessSubCategory newBusinessSubCategory = BusinessSubCategory.CreateNew(DataSourceBusinessSubCategory);
        SelectedBusinessSubCategoryID = newBusinessSubCategory.BusinessSubCatgryID;
        DataSourceBusinessCategory = BusinessCategory.GetAllBusinessCategories();
        BusinessSubCategoryViewEdit1.DataBind(DataSourceBusinessSubCategory.FindByBusinessSubCategoryID(SelectedBusinessSubCategoryID), DataSourceBusinessCategory);
        SetLayout(LayoutType.Edit);
        divexistbusinessSubcategoryname.Visible = false;
    }


    protected void ucBusinessSubCategoryList_DataSourceRequested(object sender, EventArgs evt)
    {
        DataSourceBusinessSubCategory = BusinessSubCategory.GetAllBusinessSubCategories();
        BusinessSubCategoryList1.DataSource = DataSourceBusinessSubCategory;
    }

    #endregion

    #region Control: BusinessSubCategoryViewEdit

    protected void ucBusinessSubCategoryViewEdit_DataSourceUserRequested(object sender, EventArgs evt)
    {
        BusinessSubCategoryViewEdit1.DataSourceBusinessSubCategory = DataSourceBusinessSubCategory.FindByBusinessSubCategoryID(SelectedBusinessSubCategoryID);
    }

    #endregion

    #region Save / Cancel Buttons

    protected void btnCancel_Click(object sender, ImageClickEventArgs e)
    {
        BusinessSubCategoryViewEdit1.CancelEdit();
        // If we were adding a new video...then remove the incomplete/not added new BusinessSubcategory object from the collection.
        if (DataSourceBusinessSubCategory.FindByBusinessSubCategoryID(SelectedBusinessSubCategoryID).ObjState == BOBase.ObjectState.Added)
        {
            try
            {
                DataSourceBusinessSubCategory.Remove(DataSourceBusinessSubCategory.FindByBusinessSubCategoryID(SelectedBusinessSubCategoryID));
                SelectedBusinessSubCategoryID = (DataSourceBusinessSubCategory[0] != null ? DataSourceBusinessSubCategory[0].BusinessSubCatgryID : 0);
            }
            catch
            {
            }
        }
        BusinessSubCategoryList1.DataBind(DataSourceBusinessSubCategory);
        SetLayout(LayoutType.View);
    }

    protected void btnsave_Click(object sender, ImageClickEventArgs e)
    {
        if (BusinessSubCategoryViewEdit1.EndEdit())
        {
            divexistbusinessSubcategoryname.Visible = false;
            BusinessSubCategory obj = DataSourceBusinessSubCategory.FindByBusinessSubCategoryID(SelectedBusinessSubCategoryID);
            BusinessSubCategory.Update(obj);

            // TODO: Set Feedback text...

            BusinessSubCategoryList1.DataBind(DataSourceBusinessSubCategory);
            SetLayout(LayoutType.View);
            Response.AppendHeader("Refresh", "0");
            divexistbusinessSubcategoryname.Visible = false;
        }
        else
        {
            divexistbusinessSubcategoryname.Visible = true;
        }
    }


    #endregion

    #endregion

    #region Private

    #region SetLayout

    /// <summary>
    /// Configures the UI for the specified 'uiLayoutType'.
    /// </summary>
    protected void SetLayout(LayoutType uiLayoutType)
    {
        switch (uiLayoutType)
        {
            case LayoutType.Add:
            case LayoutType.Edit:

                pnlBusinessSubCategoryList.Visible = false;
                pnlBusinessSubCategoryViewEdit.Visible = true;
                pnlButtons.Visible = true;
                break;

            case LayoutType.View:

            case LayoutType.Search:

                pnlBusinessSubCategoryList.Visible = true;
                pnlBusinessSubCategoryViewEdit.Visible = false;
                pnlButtons.Visible = false;
                break;

            default:
                throw new NotImplementedException("The specified LayoutType is not supported in this release.");
        }

        //SetInitialFocus(uiLayoutType);
    }

    #endregion

    #endregion


    
}
